import 'package:flutter_axrjc/datasource/network/api/MomentApi.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'state.dart';

class AddNoticeDialogLogic extends GetxController {
  final AddNoticeDialogState state = AddNoticeDialogState();
  @override
  void onInit() {
    super.onInit();
    state.gridlist.addAll([
      const NoticeRepeatBean(title: "每天", value: 7),
      const NoticeRepeatBean(title: "周一", value: 0),
      const NoticeRepeatBean(title: "周二", value: 1),
      const NoticeRepeatBean(title: "周三", value: 2),
      const NoticeRepeatBean(title: "周四", value: 3),
      const NoticeRepeatBean(title: "周五", value: 4),
      const NoticeRepeatBean(title: "周六", value: 5),
      const NoticeRepeatBean(title: "周天", value: 6),
    ]);
  }

  Future<void> save() async {
    if (state.label.value.isEmpty) {
      EasyLoading.showToast("请输入标签");
      return;
    }
    if (state.time.value.isEmpty) {
      EasyLoading.showToast("请选择时间");
      return;
    }
    try {
      await MomentApi.addNotice(
          ifRepeat: "${state.ifRepeat.value}",
          remindType: "${state.remindType.value}",
          name: state.label.value,
          remindTime: state.time.value,
          remindStatus: state.isopen.value ? "0" : "1");
      EasyLoading.showToast("添加成功");
      Get.back(result: true);
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      EasyLoading.showToast("$e");
    }
  }
}
